package SM.Controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import SM.BusinessLogic.DBAccess;
import SM.BusinessLogic.DBService;
import SM.BusinessLogic.SessionVariables;
import SM.UI.Controls.Table;


/**
 * Servlet implementation class ReportIssue
 */

public class Issues extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public Issues() {
        
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out = response.getWriter();
		
		String category = request.getParameter("cat");
		
		if(category.equals("all"))
			this.ShowAll(out,request);
		else if(category.equals("open"))
			this.ShowOpen(out,request);
		else if(category.equals("closed"))
			this.ShowClosed(out,request);
	}

	private void Render(PrintWriter out, String selectQuery, HttpServletRequest req)
	{
		Table issueTable = new Table("issueTab",100,"no-style",5);
		issueTable.SetWrap(true);
		issueTable.SetCellspacing(20);
		issueTable.SetColumnWidth(20);
		
		synchronized(this) { 
			
				Connection connection = null;
				PreparedStatement statement = null;
				
				try{
			/*Prolog*/
					connection = DBAccess.MakeConnection();
					statement = connection.prepareStatement(selectQuery);
					statement.setString(1, SessionVariables.GetUser(req));
					
					ResultSet resultSet = DBService.ExecuteStatement(connection, statement);
			
			/*Actual table data*/
			
					issueTable.SetDataSource(resultSet);

					out.println(issueTable.getHTML(connection));
				}catch(SQLException SQLEx)
				{
					System.out.println("SQLException qhile rendering issue\nMessage : " + SQLEx.getMessage());
				}
				finally{
			/*Epilogue*/
					DBAccess.CloseConnection(connection);
					DBAccess.CloseStatement(statement);
				}
			}
		
	}
	private void ShowClosed(PrintWriter out,HttpServletRequest request) {
		String selectQuery = "select issue_by,time,description,B.building_name,flat_no " +
				"from " +
				"Issues I,BuildingCodes B,CommitteeInfo C "+
				"where C.email = ?  and I.building_code = B.building_code and C.resp_id = I.resp_id " +
				"and I.status = 0";
		
		this.Render(out, selectQuery,request);
	}

	private void ShowOpen(PrintWriter out,HttpServletRequest request) {
		String selectQuery = "select issue_by,time,description,B.building_name,flat_no " +
				"from " +
				"Issues I,BuildingCodes B,CommitteeInfo C "+
				"where C.email = ?  and I.building_code = B.building_code and C.resp_id = I.resp_id " +
				"and I.status = 1";
		
		this.Render(out, selectQuery,request);
	}

	private void ShowAll(PrintWriter out,HttpServletRequest request) {
		String selectQuery = "select issue_by,time,description,B.building_name,flat_no " +
				"from " +
				"Issues I,BuildingCodes B,CommitteeInfo C "+
				"where C.email = ?  and I.building_code = B.building_code and C.resp_id = I.resp_id";
		
		this.Render(out, selectQuery,request);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// doPost is used to Accept request from page and Make Changes
		
	}

}
